//
//  LMLoginViewController.m
//  20.私人通讯录
//
//  Created by 张青花 on 15/8/27.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMLoginViewController.h"
#import "MBProgressHUD+NJ.h"

#define LMuser @"username"
#define LMpwd  @"password"
#define LMpwdAUTO @"pwdauto"
#define LMloginAUTO @"loginauto"

@interface LMLoginViewController ()

/**
 *
 *账号输入文本框
 */
@property (weak, nonatomic) IBOutlet UITextField *userField;
/**
 *
 *密码输入文本框
 */
@property (weak, nonatomic) IBOutlet UITextField *pwdField;

/**
 *
 *自动登录选择输入框
 */
@property (weak, nonatomic) IBOutlet UISwitch *loginSwitch;

/**
 *
 *记住密码选择输入框
 */
@property (weak, nonatomic) IBOutlet UISwitch *pwdSwitch;

/**
 *
 *用户登录按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *loginButton;


@property (strong,nonatomic) NSUserDefaults *defaults;

@end

@implementation LMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(changeBtn) name:UITextFieldTextDidChangeNotification
                 object:self.userField];
    [center addObserver:self selector:@selector(changeBtn) name:UITextFieldTextDidChangeNotification
                 object:self.pwdField];
    
     self.defaults=[NSUserDefaults standardUserDefaults];
    
    NSLog(@"viewDidLoad -- lgoinAUTO=%@",[self.defaults objectForKey:LMloginAUTO]);
    self.userField.text = [self.defaults objectForKey:LMuser];
    if ([self.defaults boolForKey:LMpwdAUTO])
    {
        self.pwdField.text = [self.defaults objectForKey:LMpwd];
        self.pwdSwitch.on = YES;
        self.loginButton.enabled = YES;
    }
    
    if ([self.defaults boolForKey:LMloginAUTO])
    {
        self.pwdField.text = [self.defaults objectForKey:LMpwd];
        self.pwdSwitch.on = YES;
        self.loginSwitch.on = YES;
        self.loginButton.enabled = YES;
        [self loginBtnOnclick:self.loginButton];

    }

}

//改变登录状态
- (void)changeBtn
{
    self.loginButton.enabled = (self.userField.text.length>0&&self.pwdField.text.length);
}

//改变自动登录状态
- (IBAction)rememberpwd:(UISwitch *)sender {
    if(self.pwdSwitch.on == YES) return;
    
    [self.loginSwitch setOn:NO animated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//自动登录
- (IBAction)autologin:(UISwitch *)sender {
    if(self.loginSwitch.on == NO) return;
    
    [self.pwdSwitch setOn:YES animated:YES];
}
/**
 *  点击登录
 *
 *  @param sender <#sender description#>
 */
- (IBAction)loginBtnOnclick:(UIButton *)sender
{
//    [MBProgressHUD showMessage:@"哥正在加载中。。。。"];
    
   MBProgressHUD *hd =  [MBProgressHUD showMessage:@"哥正在加载中。。。。" toView:nil];
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        if (![self.userField.text isEqualToString:@"qqq"]) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD  showError:@"用户名不正确"];
            return;
        }
        
        if (![self.pwdField.text isEqualToString:@"qqq"]){
            [MBProgressHUD hideHUD];
            [MBProgressHUD  showError:@"密码不正确"];
            return;
        }
        
        self.defaults=[NSUserDefaults standardUserDefaults];
        [self.defaults setObject:self.userField.text forKey:LMuser];
        [self.defaults setObject:self.pwdField.text forKey:LMpwd];
        [self.defaults setBool:self.pwdSwitch.on forKey:LMpwdAUTO];
        [self.defaults setBool:self.loginSwitch.on forKey:LMloginAUTO];
        //3.强制让数据立刻保存
         [self.defaults synchronize];
        NSLog(@"login -- lgoinAUTO=%@",[self.defaults objectForKey:LMloginAUTO]);
        
        [hd hide:YES];
        [self performSegueWithIdentifier:@"login2list" sender:nil];
    
    });
    
    
    
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *desvc = segue.destinationViewController;
    
    desvc.navigationItem.title = [NSString stringWithFormat:@"%@ 的私人通讯录",self.userField.text];
    
}




@end
